package edu.sctu.renpingan.httpclient;

import edu.sctu.renpingan.url.Final;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/6/25.
 */
public class HttpClientPost {
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

    public void httpPost() throws IOException {
        HttpPost httpPost = new HttpPost(Final.URL_CONECTION);
        List<NameValuePair> list = new ArrayList<>();
        list.add(new BasicNameValuePair("code[username]","username"));
        list.add(new BasicNameValuePair("code[password]","password"));

        httpPost.setEntity(new UrlEncodedFormEntity(list, Consts.UTF_8));
        HttpResponse httpResponse = closeableHttpClient.execute(httpPost);

        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null ){
            System.out.println(httpEntity.getContent());
            System.out.println(EntityUtils.toString(httpEntity));
        }
        closeableHttpClient.close();
    }
}
